class DrawTransformCurve {
    constructor(arg) {
        //设置唯一id 备用
        this.viewer = arg.viewer;
        this.Cesium = arg.Cesium;
        this._poly = {};
        this._positionsd = {};
        this._polyLine = {};
        this._entitys = {};
    }
  
    addEntity(params) {
        console.log(this.Cesium);
        var entiti = this.viewer.entities.add({
            
            position: this.Cesium.Cartesian3.fromDegrees(params.position.lon, params.position.lat, 10000),
            
            orientation:this.Cesium.Transforms.headingPitchRollQuaternion(this.Cesium.Cartesian3.fromDegrees(params.position.lon, params.position.lat, params.position.het),
                new this.Cesium.HeadingPitchRoll(this.Cesium.Math.toRadians(params.rotates && params.rotates.heading || 0), this.Cesium.Math.toRadians(params.rotates && params.rotates.pitch || 0), this.Cesium.Math.toRadians(params.rotates && params.rotates.roll || 0))),
            model: {
                uri: params.url || "http://localhost:3000/models/CesiumAir/Cesium_Air.glb",
                minimumPixelSize: params.minSize || 128,//最小的模型像素
                maximumScale: params.MaxSize || 10000,//最大的模型像素
            }
        })
        this._entitys[params.id] = entiti;
    }

    addtransPolyLine(params) {
        var _this = this
        var Cesium = this.Cesium;
        var viewer = this.viewer
        var PolyLinePrimitive = (function () {
            function _(positions) {
                this.options = {
                    polyline: {
                        show: true,
                        positions: [],
                        material: params.color ? Cesium.Color.fromCssColorString(params.color) : Cesium.Color.RED,
                        width: params.width || 5
                    }
                };
                this.positions = positions;
                this._init();
            }

            _.prototype._init = function () {
                var _self = this;
                var _update = function () {
                    return _self.positions;
                };
                //实时更新polyline.positions
                this.options.polyline.positions = new Cesium.CallbackProperty(_update, false);
                var pLine = viewer.entities.add(this.options);
                _this._polyLine[params.id] = pLine;
            };
            return _;
        })();
        var fromDegposition = Cesium.Cartesian3.fromDegrees(params.position.lon, params.position.lat, params.position.alt)

        if (_this._positionsd[params.id] || _this._positionsd[params.id] instanceof Array) {
            if (_this._positionsd[params.id].length >= 2) {
                if (!Cesium.defined(_this._poly[params.id])) {
                    _this._poly[params.id] = new PolyLinePrimitive(_this._positionsd[params.id]);
                } else {
                    _this._positionsd[params.id].push(fromDegposition);
                }

            } else {
                _this._positionsd[params.id].push(fromDegposition);
            }
        } else {
            _this._positionsd[params.id] = [];
            _this._positionsd[params.id].push(fromDegposition);
        }


    }



    startTransfromEnt(params) {
        var _this = this
        var Cesium = this.Cesium;
        var viewer = this.viewer
        if (this._entitys[params.id]) {
            var fromDegposition = Cesium.Cartesian3.fromDegrees(params.position.lon, params.position.lat, params.position.alt)
            this._entitys[params.id]._position._value = fromDegposition;
            if (params.ifDrawLine)
                this.addtransPolyLine(params)
            if (params.rotates) this.rotateEnt(params)
        } else {
            this.addEntity(params)
            if (params.ifDrawLine)
                this.addtransPolyLine(params)
        }
    }
 
    rotateEnt(params) {
        var _this = this
        var Cesium = this.Cesium;
        var viewer = this.viewer
        if (this._entitys[params.id]) {
            var hpr = new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(params.rotates && params.rotates.heading || 0), Cesium.Math.toRadians(params.rotates && params.rotates.pitch || 0), Cesium.Math.toRadians(params.rotates && params.rotates.roll || 0));
            var orientation = Cesium.Transforms.headingPitchRollQuaternion(this._entitys[params.id]._position._value, hpr);
            this._entitys[params.id]._orientation._value = orientation
        }
    }
   
  
    clear() {
        for (const key in this._entitys) {
            if (this._entitys.hasOwnProperty.call(this._entitys, key)) {
                const element = this._entitys[key];
                this.viewer.entities.remove(element)
                this.viewer.entities.remove(this._polyLine[key])
                this._poly[key] = undefined;
                this._positionsd[key] = undefined;
                this._polyLine[key] = undefined;
                this._entitys[key] = undefined;
            }
        }
    }

    AddPolyline(params) {
        let viewer = this.viewer;
        let Cesium = this.Cesium;
        if(params.positions.length === 0){
            alert('其提供边点!');
            return;
        }
        let entity = new Cesium.Entity({
            id: params.id || `${new Date().getTime()}线`,
            name:'线',
            show:true,
            polyline:new Cesium.PolylineGraphics ( {
                show:true,
                positions:params.positions,
                width:params.width || 5,
                material: params.material || Cesium.Color.RED
            } )
        });
        viewer.entities.add(entity);
        return entity;
    }

    AddPoint(params) {
        let viewer = this.viewer;
        let Cesium = this.Cesium;
        if(params.lon === undefined || params.lat === undefined){
            alert('请提供经纬度!');
            return;
        }
        let entity = new Cesium.Entity({
            id: params.id || `${params.lon}点`,
            name:params.name || '点',
            show:true,
            position:Cesium.Cartesian3.fromDegrees(params.lon, params.lat),
            point:new Cesium.PointGraphics ( {
                show : true,
                pixelSize : params.pixelSize || 5,
                heightReference : params.pixelSize || Cesium.HeightReference.NONE,
                color : params.color || new Cesium.Color ( 255 , 255 , 0 , 1 ),
                outlineColor : params.color || new Cesium.Color ( 0 , 0 , 0 , 0 ),
                outlineWidth : params.outlineWidth || 0,
                scaleByDistance : params.scaleByDistance || new Cesium.NearFarScalar ( 0 , 1 , 5e10 , 1 ),
                translucencyByDistance : params.translucencyByDistance || new Cesium.NearFarScalar ( 0 , 1 , 5e10 , 1 ),
                distanceDisplayCondition : params.translucencyByDistance || new Cesium.DistanceDisplayCondition(0, 4.8e10),
            } )
        });
        viewer.entities.add(entity);
        return entity;
    }
   
}
export function create(arg){
    return new DrawTransformCurve(arg)
}